#import "XMCustomSendCell.h"
#import "XMCustomSendMessage.h"

#define kGiftMsgContentViewH 78
#define kGiftImgW 54
#define kGiftImgSpace 12
#define kLabelSpace 15

@interface XMCustomSendCell ()
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *sendLa;
@property (nonatomic, strong) UILabel *nameNumLa;
@end


@implementation XMCustomSendCell

+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    
    if (model.messageDirection == MessageDirection_RECEIVE) {
        return CGSizeMake(collectionViewWidth, kGiftMsgContentViewH + extraHeight + kIntegralBtnH);
    }
    return CGSizeMake(collectionViewWidth, kGiftMsgContentViewH + extraHeight);
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setDataModel:(RCMessageModel *)model {
    [super setDataModel:model];
    XMCustomSendMessage *message = (XMCustomSendMessage *)model.content;
    
    NSString *nameNumStr = message.msgType.intValue != 2 ? kStringFormat(@"%@ x%@", message.name, message.number) : message.name;
    self.nameNumLa.text = nameNumStr;
    CGFloat nameNumW = ceil([nameNumStr widthForFont:kUIFont(14)]) + 1.f;
    CGFloat contentW = kLabelSpace + nameNumW + kGiftImgSpace + kGiftImgW + kGiftImgSpace;
    self.messageContentView.contentSize = CGSizeMake(contentW, kGiftMsgContentViewH);
    

    switch (message.msgType.intValue) {
        case 1:
            [self.imgView sd_setImageWithURL:kStringToUrl(message.imgUrl) placeholderImage:kDefaultCoverImg];

            break;
        case 2:
            [self.imgView setImage:kGetImage(@"con_invite_guard")];

            break;
        case 3:
            [self.imgView sd_setImageWithURL:kStringToUrl(message.imgUrl) placeholderImage:kDefaultCoverImg];

            break;
            
        default:
            break;
    }
    
    if (model.messageDirection == MessageDirection_RECEIVE) {
        self.sendLa.text = @"送出";
        self.integralBtn.hidden = NO;
        [self.integralBtn setTitle:kStringFormat(@"+%@积分", message.score) forState:UIControlStateNormal];
        self.sendLa.textColor = k1F1F1F;
        self.nameNumLa.textColor = k1F1F1F;

        self.imgView.frame = kFRAME(contentW - kGiftImgSpace - kGiftImgW, kGiftImgSpace, kGiftImgW, kGiftImgW);
        
        self.sendLa.frame = kFRAME(kLabelSpace, kLabelSpace, 40, 20);
        self.nameNumLa.frame = kFRAME(kLabelSpace, kGiftMsgContentViewH - kLabelSpace - 20, nameNumW, 20);
        
    } else {
        self.sendLa.text = @"送出";
        self.integralBtn.hidden = YES;
        self.sendLa.textColor = UIColor.whiteColor;
        self.nameNumLa.textColor = UIColor.whiteColor;
        
        self.imgView.frame = kFRAME(kGiftImgSpace, kGiftImgSpace, kGiftImgW, kGiftImgW);

        self.sendLa.frame = kFRAME(kGiftImgSpace * 2 + kGiftImgW, kLabelSpace, 40, 20);

        self.nameNumLa.frame = kFRAME(kGiftImgSpace * 2 + kGiftImgW, kGiftMsgContentViewH - kLabelSpace - 20, nameNumW, 20);
    }
   
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    [self showBubbleBackgroundView:YES];
//    [self.contentView addSubview:self.integralBtn];
    [self.messageContentView addSubview:self.imgView];
    [self.messageContentView addSubview:self.sendLa];
    [self.messageContentView addSubview:self.nameNumLa];
}
#pragma mark - Getter
- (UIImageView *)imgView {
    if (_imgView == nil) {
        _imgView = [[UIImageView alloc] init];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imgView;
}
- (UILabel *)sendLa {
    if (_sendLa == nil) {
        _sendLa = [[UILabel alloc] init];
        _sendLa.font = kUIFont(14);
        _sendLa.textAlignment = NSTextAlignmentLeft;
    }
    return _sendLa;
}
- (UILabel *)nameNumLa {
    if (_nameNumLa == nil) {
        _nameNumLa = [[UILabel alloc] init];
        _nameNumLa.font = kUIFont(14);
        _nameNumLa.textAlignment = NSTextAlignmentLeft;
    }
    return _nameNumLa;
}

@end
